package com.luckku.boot.controller;


import com.luckku.boot.common.api.ApiResult;


import com.luckku.boot.entity.User;
import com.luckku.boot.jwt.JwtUtil;
import com.luckku.boot.service.UserService;
import com.luckku.boot.utils.MD5Utils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.luckku.boot.jwt.JwtUtil.USER_ID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Luckku
 * @since 2022-10-09
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource(name = "userService")
    private UserService userService;


    /**
     * /user/register
     * 用户注册
     *
     * @param user User
     * @return ApiResult<Map < String, Object>>
     */
    @PostMapping(value = "/register")
    public ApiResult<Map<String, Object>> register(@Valid @RequestBody User user) {
        user = userService.executeRegister(user);
        if (ObjectUtils.isEmpty(user)) {
            return ApiResult.failed("账号注册失败");
        }
        Map<String, Object> map = new HashMap<>(16);
        map.put("user", user);
        return ApiResult.success(map);
    }


    /**
     * /user/login
     * 用户登录
     *
     * @param user User
     */
    @PostMapping(value = "/login")
    public ApiResult<Map<String, String>> login(@Valid @RequestBody User user) {
        Map<String, String> map = userService.executeLogin(user);
        if (!map.isEmpty()) {
            return ApiResult.success(map, "登录成功");
        }
        return ApiResult.failed("账号或密码错误");
    }

    /**
     * /user/getUser
     * 获取用户信息
     *
     * @param userId String
     * @return ApiResult<User>
     */
    @GetMapping(value = "/getUser")
    public ApiResult<User> getUser(@RequestParam(value = USER_ID) String userId) {
        User user = userService.getUserById(userId);
        if (user != null) {
            return ApiResult.success(user);
        }
        return ApiResult.failed("user not found");
    }

    /**
     * /user/getAllUsers
     * 获取所有用户信息
     *
     * @return ApiResult<List < User>>
     */
    @GetMapping(value = "/getAllUsers")
    public ApiResult<List<User>> getAllUsers() {
        List<User> userList = userService.list();
        if (userList != null) {
            return ApiResult.success(userList);
        }
        return ApiResult.failed("userList not found");
    }

    /**
     * /user/logout
     * 登出
     *
     * @return ApiResult<Object>
     */
    @GetMapping(value = "/logout")
    public ApiResult<Object> logOut() {
        return ApiResult.success(null, "注销成功");
    }

    /**
     * /user/updateUserInfo
     * 修改用户信息
     *
     * @return ApiResult<User>
     */
    @PutMapping(value = "/updateUserInfo")
    public ApiResult<User> updateUserInfo(@RequestBody User user) {
        userService.updateById(user);
        return ApiResult.success(user);
    }

    /**
     * /user/remove
     * 修改用户信息
     *
     * @return ApiResult<User>
     */
    @DeleteMapping(value = "/remove")
    public ApiResult<User> delete(@RequestBody User user) {
        userService.removeById(user);
        return ApiResult.success(user);
    }



}

